<template>
  <div class="service-call-statistics p-4">
    <h2 class="text-xl font-medium mb-4">数据调用统计</h2>

    <!-- 服务调用成功失败统计图 -->
    <div class="statistics-chart mb-6 bg-white p-4 rounded-lg shadow-md">
      <div class="flex items-center mb-4">
        <div class="flex items-center mr-4">
          <div class="w-3 h-3 rounded-full bg-primary mr-2"></div>
          <span>失败</span>
        </div>
        <div class="flex items-center">
          <div class="w-3 h-3 rounded-full bg-success mr-2"></div>
          <span>成功</span>
        </div>
      </div>
      <ArtBarChart
        :data="[
          { name: '学生数据子类', success: 1, fail: 1 },
          { name: '校公文服务', success: 1, fail: 0 },
          { name: 'Test01', success: 1, fail: 0 },
          { name: '校公文服务未授权目录参数', success: 1, fail: 0 }
        ]"
        :xAxisData="['学生数据子类', '校公文服务', 'Test01', '校公文服务未授权目录参数']"
        :series="[
          {
            name: '失败',
            type: 'bar',
            data: [1, 0, 0, 0],
            color: '#409EFF'
          },
          {
            name: '成功',
            type: 'bar',
            data: [1, 1, 1, 1],
            color: '#67C23A'
          }
        ]"
        :grid="{ left: '3%', right: '4%', bottom: '3%', containLabel: true }"
      />
    </div>

    <!-- 各个系统的成功和失败次数 -->
    <div class="system-statistics mb-6 bg-white p-4 rounded-lg shadow-md">
      <h3 class="text-lg font-medium mb-4">各个系统的成功和失败次数</h3>
      <ArtBarChart
        :data="[{ name: '教务', success: 1, fail: 1 }]"
        :xAxisData="['教务']"
        :series="[
          {
            name: '失败',
            type: 'bar',
            data: [1],
            color: '#409EFF'
          },
          {
            name: '成功',
            type: 'bar',
            data: [1],
            color: '#67C23A'
          }
        ]"
        :grid="{ left: '3%', right: '4%', bottom: '3%', containLabel: true }"
      />
    </div>

    <!-- 服务调用列表 -->
    <div class="service-list bg-white p-4 rounded-lg shadow-md">
      <div class="flex justify-between items-center mb-4">
        <div class="flex items-center">
          <span class="mr-4">默认排序：</span>
          <el-select v-model="searchForm.sort" placeholder="请选择" clearable>
            <el-option label="调用次数" value="callCount" />
            <el-option label="最近调用日期" value="lastCallDate" />
          </el-select>
        </div>
        <div class="flex items-center">
          <span class="mr-2">服务名称：</span>
          <el-input
            v-model="searchForm.serviceName"
            placeholder="请输入内容"
            clearable
            class="w-60"
          />
          <el-button type="primary" class="ml-4" @click="handleSearch">
            <div class="i-carbon-search mr-1"></div>查询
          </el-button>
        </div>
      </div>

      <ArtTable :data="serviceList" :loading="loading" border stripe>
        <el-table-column prop="serviceName" label="服务名称" />
        <el-table-column prop="callCount" label="调用次数" align="center" />
        <el-table-column prop="lastCallDate" label="最近调用日期" align="center" />
      </ArtTable>

      <!-- 分页 -->
      <div class="mt-4 flex justify-end">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'

  // 搜索表单
  const searchForm = reactive({
    sort: '',
    serviceName: ''
  })

  // 服务列表数据
  const loading = ref(false)
  const currentPage = ref(1)
  const pageSize = ref(10)
  const total = ref(4)

  const serviceList = ref([
    {
      serviceName: '校公文服务',
      callCount: 1,
      lastCallDate: '2019-11-15 11:32:20'
    },
    {
      serviceName: 'Test01',
      callCount: 1,
      lastCallDate: '2019-11-27 09:12:53'
    },
    {
      serviceName: '学生数据子类',
      callCount: 1,
      lastCallDate: '2023-05-24 14:47:19'
    },
    {
      serviceName: '校公文服务未授权目录参数',
      callCount: 1,
      lastCallDate: '2019-11-15 11:37:22'
    }
  ])

  // 搜索方法
  const handleSearch = () => {
    loading.value = true
    // TODO: 实现搜索逻辑
    setTimeout(() => {
      loading.value = false
    }, 1000)
  }

  // 分页方法
  const handleSizeChange = (val: number) => {
    pageSize.value = val
    handleSearch()
  }

  const handleCurrentChange = (val: number) => {
    currentPage.value = val
    handleSearch()
  }
</script>

<style scoped lang="scss">
  .service-call-statistics {
    .statistics-chart {
      height: 400px;
    }

    .system-statistics {
      height: 300px;
    }
  }
</style>
